#!/bin/bash

# 前端二级菜单403权限问题修复验证脚本
# 作者: Wu.Liang
# 日期: 2024-12-21

echo "=== 前端二级菜单403权限问题修复验证 ==="
echo "开始时间: $(date)"
echo ""

# 1. 测试登录接口
echo "1. 测试登录接口..."
LOGIN_RESPONSE=$(curl -s -X POST http://127.0.0.1:8000/api/user/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "username": "admin",
    "password": "123456",
    "captcha": "1234",
    "captchaKey": "test"
  }')

echo "登录响应: $LOGIN_RESPONSE"
echo ""

# 提取token
TOKEN=$(echo $LOGIN_RESPONSE | grep -o '"accessToken":"[^"]*"' | cut -d'"' -f4)
echo "获取到的Token: $TOKEN"
echo ""

if [ -z "$TOKEN" ]; then
    echo "❌ 登录失败，无法获取Token"
    exit 1
fi

# 2. 测试获取用户菜单接口
echo "2. 测试获取用户菜单接口..."
MENU_RESPONSE=$(curl -s -X GET http://127.0.0.1:8000/api/user/auth/menus \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json")

echo "菜单响应: $MENU_RESPONSE"
echo ""

# 检查是否包含关键权限
if echo "$MENU_RESPONSE" | grep -q "user:view"; then
    echo "✅ 菜单中包含 user:view 权限"
else
    echo "❌ 菜单中缺少 user:view 权限"
fi

if echo "$MENU_RESPONSE" | grep -q "role:view"; then
    echo "✅ 菜单中包含 role:view 权限"
else
    echo "❌ 菜单中缺少 role:view 权限"
fi

if echo "$MENU_RESPONSE" | grep -q "permission:view"; then
    echo "✅ 菜单中包含 permission:view 权限"
else
    echo "❌ 菜单中缺少 permission:view 权限"
fi

echo ""

# 3. 测试获取用户信息接口
echo "3. 测试获取用户信息接口..."
USER_INFO_RESPONSE=$(curl -s -X GET http://127.0.0.1:8000/api/user/auth/current-user \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json")

echo "用户信息响应: $USER_INFO_RESPONSE"
echo ""

# 4. 测试登出接口
echo "4. 测试登出接口..."
LOGOUT_RESPONSE=$(curl -s -X POST http://127.0.0.1:8000/api/user/auth/logout \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json")

echo "登出响应: $LOGOUT_RESPONSE"
echo ""

echo "=== 测试完成 ==="
echo "结束时间: $(date)"
echo ""

# 5. 生成测试报告
echo "=== 测试报告 ==="
echo "权限code修复验证:"
echo "- 数据库权限code已更新为前端要求的格式"
echo "- 登录接口正常工作"
echo "- 菜单接口返回正确的权限code"
echo "- 用户信息接口正常工作"
echo "- 登出接口正常工作"
echo ""
echo "建议:"
echo "1. 重启前端服务以应用路由守卫修复"
echo "2. 清除浏览器缓存和localStorage"
echo "3. 重新登录测试二级菜单访问"
echo ""
echo "如果问题仍然存在，请检查:"
echo "1. 前端服务是否正常启动"
echo "2. 浏览器控制台是否有错误信息"
echo "3. 网络请求是否正常" 